package net.lab1024.sa.admin.module.business.place.manager;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.constant.AdminCacheConst;
import net.lab1024.sa.admin.module.business.category.dao.CategoryDao;
import net.lab1024.sa.admin.module.business.category.domain.entity.CategoryEntity;
import net.lab1024.sa.admin.module.business.place.dao.PlaceDao;
import net.lab1024.sa.admin.module.business.place.domain.entity.PlaceEntity;
import net.lab1024.sa.admin.module.business.place.domain.vo.PlaceTreeVO;
import net.lab1024.sa.admin.module.business.place.dao.RSchoolDao;
import net.lab1024.sa.base.common.constant.StringConst;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类目 查询 缓存
 *
 * @Author 1024创新实验室: 胡克
 * @Date 2021/08/05 21:26:58
 
 
  * @Copyright  cpedu
 */
@Service
@Slf4j
public class PlaceCacheManager {


    @Resource
    private CategoryDao categoryDao;

    @Resource
    private PlaceDao placeDao;



//    /**
//     * 根据类目id 移除缓存
//     */
//    @CacheEvict(value = {AdminCacheConst.Category.CATEGORY_ENTITY, AdminCacheConst.Category.CATEGORY_SUB, AdminCacheConst.Category.CATEGORY_TREE}, allEntries = true)
//    public void removeCache() {
//        log.info("clear CATEGORY ,CATEGORY_SUB ,CATEGORY_TREE");
//    }
//
//    /**
//     * 查詢类目
//     *
//     */
//    @Cacheable(AdminCacheConst.Category.CATEGORY_ENTITY)
//    public CategoryEntity queryCategory(Long categoryId) {
//        return categoryDao.selectById(categoryId);
//    }
//
//    /**
//     * 查询类目 子级
//     *
//     */
//    @Cacheable(AdminCacheConst.Category.CATEGORY_SUB)
//    public List<CategoryEntity> querySubCategory(Long categoryId) {
//        return categoryDao.queryByParentId(Lists.newArrayList(categoryId), false);
//    }
//
//
    /**
     * 查询类目 层级树
     * 优先查询缓存
     */
//    @Cacheable(AdminCacheConst.Category.CATEGORY_TREE)
    public List<PlaceTreeVO> queryPlaceTree(Long parentId) {
        List<PlaceEntity> allCategoryEntityList = placeDao.queryByType(false);
        List<PlaceEntity> placeEntityList = allCategoryEntityList.stream().filter(e -> e.getParentId().equals(parentId)).collect(Collectors.toList());
        List<PlaceTreeVO> treeList = SmartBeanUtil.copyList(placeEntityList, PlaceTreeVO.class);
        treeList.forEach(e -> {
            e.setLabel(e.getPlaceName());
            e.setValue(e.getPlaceId());
            e.setPlaceFullName(e.getPlaceName());
        });
        // 递归设置子类
        this.queryAndSetSubCategory(treeList, allCategoryEntityList);
        return treeList;
    }
    private String findId(List<PlaceTreeVO> tree, Long placeId){
        if (!CollectionUtils.isEmpty(tree) && placeId!=null) {
        List<PlaceTreeVO> childrens = Lists.newArrayList();
        for (PlaceTreeVO e : tree) {
            if (e.getPlaceId().equals(placeId)) {
                return e.getPlaceFullName();
            }else{
                childrens.addAll(e.getChildren());
            }
        }
        return findId(childrens, placeId);
        }
        return null;
    }
    //    @Cacheable(AdminCacheConst.Category.CATEGORY_TREE)
    public String queryPlaceTreeByName(Long placeId) {
        List<PlaceTreeVO> treeList = queryPlaceTree(0L);
        return findId(treeList, placeId);
    }
    /**
     * 递归查询设置类目子类
     * 从缓存查询子类
     *
     */
    private void queryAndSetSubCategory(List<PlaceTreeVO> treeList, List<PlaceEntity> allCategoryEntityList) {
        if (CollectionUtils.isEmpty(treeList)) {
            return;
        }
        List<Long> parentIdList = treeList.stream().map(PlaceTreeVO::getValue).collect(Collectors.toList());
        List<PlaceEntity> placeEntityList = allCategoryEntityList.stream().filter(e -> parentIdList.contains(e.getParentId())).collect(Collectors.toList());
        Map<Long, List<PlaceEntity>> placeSubMap = placeEntityList.stream().collect(Collectors.groupingBy(PlaceEntity::getParentId));
        treeList.forEach(e -> {
            List<PlaceEntity> childrenEntityList = placeSubMap.getOrDefault(e.getValue(), Lists.newArrayList());
            List<PlaceTreeVO> childrenVOList = SmartBeanUtil.copyList(childrenEntityList, PlaceTreeVO.class);
            childrenVOList.forEach(item -> {
                item.setLabel(item.getPlaceName());
                item.setValue(item.getPlaceId());
                item.setPlaceFullName(e.getPlaceFullName() + StringConst.SEPARATOR_SLASH + item.getPlaceName());
            });
            // 递归查询
            this.queryAndSetSubCategory(childrenVOList, allCategoryEntityList);
            e.setChildren(childrenVOList);
        });
    }
}
